#include "types.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>


typedef enum { FAUX, VRAI } Booleen;
Booleen encore = VRAI;

FILE *fTXT;
FILE* fcompt;

int i;
int k;
char buf[TAILLEMAX]; 
char compt[TAILLE_TAMPON] ;
char RDV[NRDV][TAILLEMAX];
int comptint;
char c;
char *d;
char *e;

void lecturetxt() {

  if ( ( fcompt = fopen ("../RDVtxt/compt.txt", "r" ) ) == NULL ) {
    fprintf ( stderr, "Erreur ouverture fichier TXT\n" );
    exit ( EXIT_FAILURE );
  }
  
  
  while ( (fgets(compt, TAILLE_TAMPON, fcompt)) !=NULL )
    
    comptint=atoi(compt);
  
  fclose(fcompt);
  
  
  if ( ( fTXT = fopen (RDV[comptint], "r" ) ) == NULL ) {
    fprintf ( stderr, "Erreur ouverture fichier TXT\n" );
    exit ( EXIT_FAILURE );
  }
  
  while ( encore ) {
    
    c=fgetc(fTXT);	
    d=&c;
    while ( c!=EOF && c!='<' ) {
      infos.titre = strcat(infos.titre,d);	
      c=fgetc(fTXT);
      d=&c;
    }
    
    switch (c) {
    case EOF : encore=FAUX;
      break;
    case '<' : while ( ( c = fgetc(fTXT) ) != '<') {
	d=&c; 
	(infos.description) = strcat((infos.description),d);
      }
      
      switch (c) {
      case EOF : encore=FAUX;
	break;
      case '<' : while ( ( c = fgetc(fTXT) ) != '<' ) {
	  d=&c;
	  (infos.nomRespo) = strcat((infos.nomRespo),d);
	}
	switch (c) {
	case EOF : encore=FAUX;
	  break;
	case '<' : while ( ( c = fgetc(fTXT) ) != '<' ) {
	    d=&c;
	    (infos.mailRespo) = strcat(infos.mailRespo,d);
	  }
	  
	}
	
	
      }
      
    }
  }
  while ( encore ) {
    for (k=0;k<NPART;k++) {
      c=fgetc(fTXT);	
      d=&c;
      
      while ( c!=EOF && c!='<' ) {
	
	if ( sprintf (buf,"%d",(participants[k].num)) < 0 ) {
	  fprintf ( stderr, "Erreur de sortie fprintf\n" );
	  exit ( EXIT_FAILURE );
	}	
	participants[k].num = atoi(strcat(buf,d));	
	c=fgetc(fTXT);
	d=&c;
      }
      
      switch (c) {
      case EOF : encore=FAUX;
	break;
      case '<' : while ( ( c = fgetc(fTXT) ) != '<') {
	  d=&c;
	  (participants[k].nom) = strcat((participants[k].nom),d);
	}
	
	switch (c) {
	case EOF : encore=FAUX;
	  break;
	case '<' : while ( ( c = fgetc(fTXT) ) != '<') {
	    d=&c;
	    (participants[k].mail) = strcat((participants[k].mail),d);
	  }
	  
	  
	}
      }
      
    }
  }	
  
  while ( encore ) {
    for (k=0;k<NCREN;k++) {
      c=fgetc(fTXT);	
      d=&c;
      
      while ( c!=EOF && c!='<' ) {
	if ( sprintf (buf,"%d",(creneaux[k].num)) < 0 ) {
	  fprintf ( stderr, "Erreur de sortie fprintf\n" );
	  exit ( EXIT_FAILURE );
	}	
	creneaux[k].num = atoi(strcat(buf,d));		
	c=fgetc(fTXT);
	d=&c;
      }
      
      switch (c) {
      case EOF : encore=FAUX;
	break;
      case '<' : while ( ( c = fgetc(fTXT) ) != '<') {
	  d=&c;
	  if ( sprintf (buf,"%d",(creneaux[k].annee)) < 0 ) {
	    fprintf ( stderr, "Erreur de sortie fprintf\n" );
	    exit ( EXIT_FAILURE );
	  }	
	  creneaux[k].annee = atoi(strcat(buf,d));	
	  
	}
	
	switch (c) {
	case EOF : encore=FAUX;
	  break;
	case '<' : while ( ( c = fgetc(fTXT) ) != '<') {
	    d=&c;
	    if ( sprintf (buf,"%d",(creneaux[k].mois)) < 0 ) {
	      fprintf ( stderr, "Erreur de sortie fprintf\n" );
	      exit ( EXIT_FAILURE );
	    }	
	    creneaux[k].mois = atoi(strcat(buf,d));
	  }
	  
	  switch (c) {
	  case EOF : encore=FAUX;
	    break;
	  case '<' : while ( ( c = fgetc(fTXT) ) != '<') {
	      d=&c;
	      if ( sprintf (buf,"%d",(creneaux[k].jour)) < 0 ) {
		fprintf ( stderr, "Erreur de sortie fprintf\n" );
		exit ( EXIT_FAILURE );
	      }	
	      creneaux[k].jour = atoi(strcat(buf,d));
	    }
	    
	    switch (c) {
	    case EOF : encore=FAUX;
	      break;
	    case '<' : while ( ( c = fgetc(fTXT) ) != '<') {
		d=&c;
		if ( sprintf (buf,"%d",(creneaux[k].heure)) < 0 ) {
		  fprintf ( stderr, "Erreur de sortie fprintf\n" );
		  exit ( EXIT_FAILURE );
		}	
		creneaux[k].heure = atoi(strcat(buf,d));
	      }
	      
	      switch (c) {
	      case EOF : encore=FAUX;
		break;
	      case '<' : while ( ( c = fgetc(fTXT) ) != '<') {
		  d=&c;
		  if ( sprintf (buf,"%d",(creneaux[k].min)) < 0 ) {
		    fprintf ( stderr, "Erreur de sortie fprintf\n" );
		    exit ( EXIT_FAILURE );
		  }	
		  creneaux[k].min = atoi(strcat(buf,d));
		}
		
		
		
		
	      }
	    }
	  }
	  
	}	   
      }	   
    }
    
  } 
fclose(fTXT); 
}
